package com.udima.reglocation;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

public class ConfTrackActivity extends Activity {

    private Button btnSave;
    private Button btnGet;
    private EditText inputName;
    private EditText inputPeriod;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.conf_track_activity);      

        inputName     = (EditText) findViewById(R.id.inputName);
        inputPeriod   = (EditText) findViewById(R.id.inputPeriod);
        btnSave       = (Button) findViewById(R.id.btnSave);
        btnGet        = (Button) findViewById(R.id.btnGet);
        
        //Asignacion de valores con la configuracion actual
        //inputPeriod.setText(String.valueOf(MainMenuActivity.config.getPeriod()));
        
        btnSave.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View view) {
            	//MainMenuActivity.config.setPeriod(Integer.parseInt(inputPeriod.getText().toString()));
            }
        });
        
        btnGet.setOnClickListener(new View.OnClickListener() {
        	 
            public void onClick(View view) {
            	String uid = "uid002";
            	//Configuration conf = new Configuration();          	
            	//conf.getConf(ConfTrackActivity.this,uid);
            	
                // Llamada a AsynTask para realizar las operaciones de red en un hilo independiente
                new HttpAsyncTask().execute("http://10.0.2.2:8080/GeoUdimaServer/rest/users/"+uid);
            	
            	//inputName.setText(uid);
            }
        });
    } 
    
    public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {
 
            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();
 
            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
 
            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();
 
            // convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";
 
        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }
 
        return result;
    }
    
    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;
 
        inputStream.close();
        return result;
 
    }
    
    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
 
            return GET(urls[0]);
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
            try {
				JSONObject json = new JSONObject(result);
				inputName.setText(json.getString("lastName"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
       }
    }
}